package com.ruoyi.quartz.task;

import com.ruoyi.moon.domain.MoonCompany;
import com.ruoyi.moon.domain.MoonNotice;
import com.ruoyi.moon.domain.MoonNoticeTemplate;
import com.ruoyi.moon.domain.MoonReviews;
import com.ruoyi.moon.mapper.MoonReviewsMapper;
import com.ruoyi.moon.service.IMoonCompanyService;
import com.ruoyi.moon.service.IMoonNoticeService;
import com.ruoyi.moon.service.IMoonNoticeTemplateService;
import com.ruoyi.quartz.domain.enums.TaskEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    @Autowired
    private IMoonNoticeService moonNoticeService;
    @Autowired
    private IMoonNoticeTemplateService moonNoticeTemplateService;
    @Autowired
    private MoonReviewsMapper moonReviewsMapper;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
//        TaskEnum code = TaskEnum.fromCode(params);
//        switch (code) {
//            case AUDIT:
//               MoonReviews moonReviews = new MoonReviews();
//                List<MoonReviews> moonReviews1 = moonReviewsMapper.selectMoonReviewsList(moonReviews);
//                for (MoonReviews moonReviews2 : moonReviews1) {
//                    MoonNoticeTemplate moonNoticeTemplate = new MoonNoticeTemplate();
//                    moonNoticeTemplate.setTemplateType(1L);
//                    moonNoticeTemplate.setTemplateState(0);
//                    List<MoonNoticeTemplate> noticeTemplates = moonNoticeTemplateService.selectMoonNoticeTemplateList(moonNoticeTemplate);
//                    for (MoonNoticeTemplate template : noticeTemplates) {
//                        MoonNotice notice = new MoonNotice();
//                        String noticeContent = template.getTemplateContent();
//                        if (moonReviews2.getCompanyName() != null)
//                            noticeContent = noticeContent.replaceAll("\\$\\{quest\\}", moonReviews2.getCompanyName());
//                        notice.setNoticeContent(noticeContent);
//                        notice.setNoticeTitle(template.getTemplateTitle());
//                        if (moonReviews2.getUserId() != null) {
//                            notice.setUserId(Long.valueOf(moonReviews2.getUserId()));
//                            moonNoticeService.insertMoonNotice(notice);
//                        }
//                    }
//                    {
//
//                    }
//                }
//                break;
//
//            default:
//                break;
//        }


        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        MoonReviews moonReview = new MoonReviews();
        moonReview.setAuditStatus("2");
        List<MoonReviews> moonReviews = moonReviewsMapper.selectMoonReviewsList(moonReview);

        if(CollectionUtils.isNotEmpty(moonReviews)){
            //发送消息
            MoonNoticeTemplate moonNoticeTemplate = new MoonNoticeTemplate();
            moonNoticeTemplate.setTemplateType(1L);
            moonNoticeTemplate.setTemplateState(0);
            List<MoonNoticeTemplate> noticeTemplates = moonNoticeTemplateService.selectMoonNoticeTemplateList(moonNoticeTemplate);
            for(MoonReviews reviews : moonReviews){
                reviews.setAuditStatus("0");
                moonReviewsMapper.updateMoonReviews(reviews);
                for (MoonNoticeTemplate template : noticeTemplates) {
                    MoonNotice notice = new MoonNotice();
                    String noticeContent = template.getTemplateContent();
                    if (reviews.getCompanyName() != null) {
                        noticeContent = noticeContent.replaceAll("\\$\\{companyName\\}", reviews.getCompanyName())
                                .replaceAll("\\$\\{statusText\\}", "审核通过");
                    }
                    notice.setNoticeContent(noticeContent);
                    notice.setNoticeTitle(template.getTemplateTitle());
                    if (reviews.getUserId() != null) {
                        notice.setUserId(Long.valueOf(reviews.getUserId()));
                        moonNoticeService.insertMoonNotice(notice);
                    }
                }
            }
        }

    }
}
